<?php

namespace falcon5\inc\Http;

/**
 * Class Request
 * @package falcon5\inc\Http
 */
class Request
{
    private $get;
    private $post;
    private $action;

    public function __construct()
    {
        $this->get = $_GET;
        $this->post = $_POST;
        $this->action = $_REQUEST['action'] ?? null;
        
    }

    /**
     * fetch get params
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function get(string $key, $default = null)
    {
        if (isset($this->post[$key])) {
            return $this->post[$key];
        }
        
        if (isset($this->get[$key])) {
            return $this->get[$key];
        }
        
        return $default;
    }

    /**
     * fetch post params
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function post(string $key, $default = null)
    {
        return $this->post[$key] ?? $default;
    }

    /**
     * fetch all post params
     * @return array
     */
    public function all(): array
    {
        return $this->post;
    }

    /**
     * fetch method
     * @return string
     */
    public function getMethod(): string
    {
        return $_SERVER['REQUEST_METHOD'];
    }

    /**
     * set action
     * @param string $action
     */
    public function setAction(string $action)
    {
        $this->action = $action;
    }

    /**
     * get action
     * @return string|null
     */
    public function getAction()
    {
        return $this->action;
    }
    
    /**
     * set params
     * @param string $key
     * @param mixed $value
     * @return void
     */
    public function setParam(string $key, $value)
    {
        $this->post[$key] = $value;
    }
} 